package com.eastmoney.emis.edda.infrastructure.edda.repository;

import com.eastmoney.emis.edda.domain.entity.EddaApply;
import com.eastmoney.emis.edda.domain.entity.EddaBind;
import com.eastmoney.emis.edda.domain.repository.HsbcEddaRepository;
import com.eastmoney.emis.edda.domain.valueobject.EddaApplyStatus;
import com.eastmoney.emis.edda.domain.valueobject.TimeFormatter;
import com.eastmoney.emis.edda.infrastructure.edda.mapper.EmhkEddaApplyMapper;
import com.eastmoney.emis.edda.infrastructure.edda.mapper.EmhkEddaBindMapper;
import com.eastmoney.emis.edda.infrastructure.edda.po.EmhkEddaApplyPo;
import com.eastmoney.emis.edda.infrastructure.edda.po.EmhkEddaBindPo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @description: Description
 * @projectName: EDDA-HSBC
 * @package: com.eastmoney.emis.edda.domain.repository
 * @author: 汪程程
 * @date: 2022/9/29 5:52 PM
 * @version: 1.0
 */
@Repository
public class HsbcEddaRepositoryImpl implements HsbcEddaRepository {

    @Autowired
    private EmhkEddaBindMapper eddaBindMapper;

    @Autowired
    private EmhkEddaApplyMapper eddaApplyMapper;


    @Override
    public void addEddaBind(EddaBind eddaBind) {
        eddaBind.setEid(eddaApplyMapper.getEid(eddaBind.getClientId()));
        eddaBindMapper.addEddaBind(eddaBind.getClientId(), new EmhkEddaBindPo(eddaBind));
    }

    @Override
    public void save(EddaBind eddaBind) {
        if (eddaBind.getEid() == null) {
            addEddaBind(eddaBind);
            return;
        }
        updateEddaBind(eddaBind);
    }

    @Override
    public void save(EddaApply eddaApply) {
        if (eddaApply.getEid() == null) {
            addEddaApply(eddaApply);
            return;
        }
        updateEddaApply(eddaApply);
    }

    private void updateEddaBind(EddaBind eddaBind) {
        eddaBindMapper.update(eddaBind.getClientId(), new EmhkEddaBindPo(eddaBind));
    }

    private void updateEddaApply(EddaApply eddaApply) {
        eddaApplyMapper.update(eddaApply.getClientId(), new EmhkEddaApplyPo(eddaApply));
    }

    @Override
    public void addEddaApply(EddaApply eddaApply) {
        eddaApply.setEid(eddaApplyMapper.getEid(eddaApply.getClientId()));
        eddaApplyMapper.addEddaApply(eddaApply.getClientId(), new EmhkEddaApplyPo(eddaApply));
    }


    @Override
    public EddaBind findEddaBindByClientIdAndBankAccount(String clientId, String bankCode, String bankAccount) {
        EmhkEddaBindPo emhkEddaBindPo = eddaBindMapper.findByClientIdAndBankAccount(clientId, bankCode, bankAccount);
        return emhkEddaBindPo != null ? new EddaBind(emhkEddaBindPo) : null;
    }

    @Override
    public EddaApply findEddaApplyInProgress(String clientId, String bankCode, String bankAccount) {
        List<EmhkEddaApplyPo> emhkEddaApplyPoList = eddaApplyMapper.findByClientIdAndBankAccount(clientId, bankCode, bankAccount);
        return emhkEddaApplyPoList
                .stream().filter(r -> EddaApplyStatus.PROCESSING.value().equals(r.STATUS))
                .findFirst().map(EddaApply::new)
                .orElse(null);
    }



    @Override
    public EddaBind findEddaBindByClientIdAndMandateId(String clientId, String mandateId) {
        EmhkEddaBindPo emhkEddaBindPo = eddaBindMapper.findByClientIdAndMandateId(clientId, mandateId);
        return emhkEddaBindPo != null ? new EddaBind(emhkEddaBindPo) : null;
    }

    @Override
    public EddaApply findEddaApplyByClientIdAndRegisterId(String clientId, String registerId) {
        EmhkEddaApplyPo emhkEddaApplyPo = eddaApplyMapper.findByClientIdAndRegisterId(clientId, registerId);
        return emhkEddaApplyPo == null ? null : new EddaApply(emhkEddaApplyPo);
    }

    @Override
    public EddaApply findEddaApplyByEid(String eid) {
        EmhkEddaApplyPo eddaApplyPo = eddaApplyMapper.findByEid(eid);
        return eddaApplyPo == null ? null : new EddaApply(eddaApplyPo);
    }

    @Override
    public EddaApply findActiveEddaApplyByClientIdAndMandateId(String clientId, String mandateId) {
        EmhkEddaApplyPo emhkEddaApplyPo = eddaApplyMapper.findByClientIdAndMandateIdAndStatus(clientId, mandateId, EddaApplyStatus.SUCCESS.value());
        return emhkEddaApplyPo == null ? null : new EddaApply(emhkEddaApplyPo);
    }

    @Override
    public int getEddaApplyTimesDailyByClientId(String clientId) {
        return eddaApplyMapper.getEddaApplyTimesDailyByClientId(clientId, TimeFormatter.YYYY_MM_DD.format(LocalDate.now()));
    }

    @Override
    public int getEddaApplyTimesDailyByClientIdAndStartTime(String clientId, LocalDateTime dateTime) {
        return eddaApplyMapper.getEddaApplyTimesDailyByClientIdAndEITimeGreaterThan(clientId, dateTime);
    }

    @Override
    public EddaApply findFirstDailyByClientId(String clientId) {
        EmhkEddaApplyPo emhkEddaApplyPo = eddaApplyMapper.findFirstDailyByClientId(clientId, TimeFormatter.YYYY_MM_DD.format(LocalDate.now()));
        return emhkEddaApplyPo == null ? null : new EddaApply(emhkEddaApplyPo);
    }

    @Override
    public List<EddaApply> findAllUncompleted() {
        return eddaApplyMapper.findAllUncompleted()
                .stream()
                .map(EddaApply::new)
                .collect(Collectors.toList());
    }


}
